/**
 * Sample React Native App By Wyh
 * Created by wyh on 15/12/23.
 */
'use strict';

import React,{
  StyleSheet,
  Navigator,
  Platform,
} from 'react-native';

import {msg} from 'iflux-native';


//Home
import Home from './home';


/**
 * 路由映射
 */
const routeMapper = {

  //Home
  Home: Home,

}


class Route extends React.Component{


  constructor(props = {}){
    super(props);

    this._pushRoute = this._pushRoute.bind(this);
    this._popRoute = this._popRoute.bind(this);
    this._popToTop = this._popToTop.bind(this);
    this._popToRoute = this._popToRoute.bind(this);
    this._replaceRoute = this._replaceRoute.bind(this);
    this._replaceAtIndex = this._replaceAtIndex.bind(this);
  }


  /**
   * 事件驱动Navigator,这样不需要通过属性透传到natigator到各个子组件
   */
  componentDidMount(){
    msg
      .on('route:goToNext', this._pushRoute)
      .on('route:backToLast', this._popRoute)
      .on('route:backToTop', this._popToTop)
      .on('route:popToRoute', this._popToRoute)
      .on('route:replaceRoute', this._replaceRoute)
      .on('route:replaceAtIndex', this._replaceAtIndex);
  }


  componentWillUnmount(){
    msg
      .removeListener('route:goToNext', this._pushRoute)
      .removeListener('route:backToLast', this._popRoute)
      .removeListener('route:backToTop', this._popToTop)
      .removeListener('route:popToRoute', this._popToRoute)
      .removeListener('route:replaceRoute', this._replaceRoute)
      .removeListener('route:replaceAtIndex', this._replaceAtIndex);
  }


  render(){
    return (
      <Navigator
        ref={(navigator) => this._navigator = navigator}
        style={{backgroundColor: '#000'}}
        sceneStyle={{backgroundColor: '#FFF'}}
        initialRoute={{sceneName:this.props.isLogin ? 'Home' : 'Home'}}
        configureScene={(route) => {
          if(route.sceneConfig){
            return route.sceneConfig
          }else{
            return Navigator.SceneConfigs.PushFromRight;
          }
        }}
        renderScene={(route, navigator) => {
          const {sceneName, sceneConfig, ...params} = route;
          console.log('sceneName==' + sceneName);
          if(sceneName != 'Home'){
            this._lastSceneName = sceneName;
            this._sceneParams = params;
          }

          const Scene = routeMapper[sceneName];
          return <Scene {...params}/>
        }}
      />
    )
  }


  /**
   * push路由
   * @param sceneParam
   * @private
   */
  _pushRoute(sceneParam){
    if(__DEV__){
      console.log('正在push路由:', sceneParam);
    }

    this._navigator.push(sceneParam);
  }


  /**
   * pop路由
   * @private
   */
  _popRoute(){
    if(__DEV__){
      console.log('正在pop路由')
    }

    this._navigator.pop();
  }


  /**
   * 返回第一个视图
   * @private
   */
  _popToTop(){
    if(__DEV__){
      console.log('route popToTop');
    }

    this._navigator.popToTop();
  }


  /**
   * 返回到指定视图
   * @param sceneParam
   * @private
   */
  _popToRoute(sceneParam){
    if(__DEV__){
      console.log('route  popToRoute');
    }

    this._navigator.popToRoute(sceneParam);
  }


  /**
   * 替换当前视图
   * @param sceneParam
   * @private
   */
  _replaceRoute(sceneParam){
    if (__DEV__) {
      console.log('route replaceRoute');
    }

    this._navigator.replace(sceneParam);
  }


  _replaceAtIndex(sceneParam, index){
    if(__DEV__){
      console.log('route _replaceAtIndex');
    }

    this._navigator.replaceAtIndex(sceneParam, index);
  }

}


module.exports = Route;
